Fix COPY cleanup when a records generator raises - #1358
Open
CodingCossack wants to merge 1 commit into
Open
Conversation
Use a fixed short CopyFail reason instead of formatting the application exception. PostgreSQL limits this message to 10000 bytes, including its length field and trailing NUL. An oversized reason loses protocol synchronisation and can leave transaction rollback waiting indefinitely. Keep the original application exception intact, including when formatting or encoding it would fail. Cover encoded-byte boundaries, multibyte text, sync and async generators, COPY atomicity, rollback and connection reuse. Fixes MagicStack#1354
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1354.
When a records generator raises, COPY cleanup currently sends
str(e)as the CopyFail reason. PostgreSQL limits the message to 10,000 bytes, including the four-byte length field and trailing NUL. A reason longer than 9,995 encoded bytes therefore produces an invalid message, closes the connection and can leave transaction rollback waiting indefinitely.Use the fixed ASCII reason
COPY source failedinstead. This avoids oversized messages and secondary failures while formatting or encoding exception text. Keep the existing cancellation flow and bare re-raise, preserving the original application exception on this path. PostgreSQL now receives a generic reason; the caller's exception is not truncated or replaced.Regression coverage exercises synchronous and asynchronous generators, implicit and explicit transactions, ASCII and multibyte byte-length boundaries, failing
__str__, and unencodable exception text. Each case sends COPY data before failing, checks exception identity and rollback/atomicity, then performs a successful COPY and query on the same connection and closes it normally.Validation
Tested locally on macOS arm64 with CPython 3.13.14 and PostgreSQL 16.14:
python -m unittest -v tests.suite: successful; 313 tests run, four skipped, including the suite's flake8 and mypy checks.USE_UVLOOP=1 python -m unittest -v tests.test_copy: 26 tests passed.PYTHONASYNCIODEBUG=1;git diff --checkpasses.This change does not attempt a general connection-loss/cancellation-state repair. The full upstream CI matrix was not run locally.
AI assistance
OpenAI Codex was used for implementation and local test execution. ChatGPT was used for independent source/patch review and preparation of this description.